package net.sxjr.protal.igperformancepay.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.sxjr.common.Response;
import net.sxjr.mbg.entity.IgContractManage;
import net.sxjr.mbg.entity.IgPurchaseOfficeSupplies;
import net.sxjr.mbg.entity.IgWageManage;
import net.sxjr.mbg.mapper.IgWageManageMapper;
import net.sxjr.protal.igperformancepay.params.IgWangeManageParams;
import net.sxjr.protal.mapconverter.Converter;
import net.sxjr.protal.publicservice.PublicService;
import net.sxjr.protal.user.dao.LoginUserInfo;
import net.sxjr.protal.user.dao.UserInfo;
import net.sxjr.protal.user.service.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：cuixc
 * @date ：Created in 2020/12/14 13:30
 * @description：
 * @modified By：
 */
@Slf4j
@Service
public class IgWangeManageService {
    @Autowired
    private LoginService loginService;
    @Autowired
    private IgWageManageMapper wageManageMapper;
    @Autowired
    private PublicService publicService;
    public Response wangeManageAdd(IgWangeManageParams params) {
        log.info("薪酬绩效管理 编辑 {}", JSON.toJSONString(params));
        //获取当前登入人
        LoginUserInfo currentUser = loginService.getCurrentUser();
        UserInfo userinfo = loginService.getUserinfo(currentUser.getAccount());
        IgWageManage igWageManage = Converter.INSTANCE.wangmanageParams2Dao(params);
        if(StringUtils.isBlank(params.getWageManageId())){
            igWageManage.setCreateDate(new Date());
            igWageManage.setCreateOp(userinfo.getUserId());
            igWageManage.setDeptId(userinfo.getDeptId());
            igWageManage.insert();
        }else{
            igWageManage.updateById();

            publicService.setLinkConfirm(igWageManage,loginService.getCurrentUser());
        }
        publicService.writeSmsManage(publicService.getLinkCode(igWageManage),igWageManage.getSuperviseCode());

        return Response.success(Converter.INSTANCE.wangemanageDao2Params(igWageManage));
    }

    public Response list() {
        log.info("薪酬绩效管理 列表");
        List<IgWageManage> igWageManages = wageManageMapper.selectList(
                new LambdaQueryWrapper<IgWageManage>().ne(IgWageManage::getDeleteFlag,1).or().isNull(IgWageManage::getDeleteFlag)
                        .orderByDesc(IgWageManage::getSuperviseCode));
        return Response.success(Converter.INSTANCE.wangemanageDao2Params(igWageManages));
    }
}
